package com.future.config;


import com.future.filter.JwtAuthenticationTokenFilter;
import com.future.handler.AccessDeniedHandlerImpl;
import com.future.handler.AuthenticationEntryPointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;


/**
 * SpringSecurity配置类
 * @EnableGlobalMethodSecurity(prePostEnabled = true)
 * 用于开启四个方法级别的权限控制的注解 如@PreAuthorize("hasAuthority('system:dept:list')")
 */
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)

public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
    @Autowired
    AuthenticationEntryPointImpl authenticationEntryPoint;
    @Autowired
    AccessDeniedHandlerImpl accessDeniedHandler;

    //创建BCryptPasswordEncoder注入容器
    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder()
    {
        return new BCryptPasswordEncoder();
    }


    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http    //关闭内置的/logout接口
                .logout().disable()
                //关闭csrf
                .csrf().disable()
                //不通过Session获取SecurityContext
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                // 允许匿名访问
                .antMatchers(
                        "/accountLogin",
                        "/emailLogin",
                        "/register",
                        "/sendCode",
                        "/forgetPassword"

                ).anonymous()

                .antMatchers(
                        "/swagger-resources/**",
                        "/swagger-ui.html",
                        "/v2/api-docs",
                        "/webjars/**",
                        "/swagger-ui/**",
                        "/article/**",
                        "/tag/**",
                        "/article/approved",
                        "/knowledge/get/{id}",
                        "/knowledge/list",
                        "/knowledge/random",
                        "/DoctorList",
                        "/media/type"
                ).permitAll()
                //.anonymous()未登录可以访问,已登录无法访问
                //.permitAll(),不管登没登陆都可以访问(适用于静态资源)
                // 除上面外的所有请求全部需要鉴权认证
                .anyRequest().authenticated();

        //JWT过滤器要放在UsernamePasswordAuthenticationFilter之前
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
        //配置认证授权的异常处理器
        http.exceptionHandling()
                .authenticationEntryPoint(authenticationEntryPoint)
                .accessDeniedHandler(accessDeniedHandler);
    }
}
